package MapReduce.TopN;
import java.util.Objects;

// 自定义辅助类，用于保存词频和词语
public class WordFrequencyPair implements Comparable<WordFrequencyPair> {
    private final int frequency;
    private final String word;

    public WordFrequencyPair(int frequency, String word) {
        this.frequency = frequency;
        this.word = word;
    }

    public int getFrequency() {
        return frequency;
    }

    public String getWord() {
        return word;
    }
//    注意，在该实现中，比较和哈希计算方法均考虑了词频和词语。这样可以确保在构建TreeMap时，不会出现数据覆盖的问题。
    @Override
    public int compareTo(WordFrequencyPair other) {
        if (this.frequency != other.frequency) {
            return this.frequency - other.frequency;
        } else {
            return this.word.compareTo(other.word);
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        WordFrequencyPair other = (WordFrequencyPair) obj;
        return frequency == other.frequency && Objects.equals(word, other.word);
    }

    @Override
    public int hashCode() {
        return Objects.hash(frequency, word);
    }
}